import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.css'

import {DeleteRequest, GetRequest, PostRequest, PutRequest} from './utils/api'
import {initMenu} from './utils/menus'
import {downloadRequest} from './utils/download'

Vue.config.productionTip = false

Vue.use(ElementUI, {size: 'small'});
//插件形式使用请求
Vue.prototype.postRequest = PostRequest
Vue.prototype.getRequest = GetRequest
Vue.prototype.putRequest = PutRequest
Vue.prototype.deleteRequest = DeleteRequest
Vue.prototype.downloadRequest = downloadRequest

router.beforeEach((to, from, next) => {
    //判断是否登录
    if (window.sessionStorage.getItem("tokenStr")) {
        initMenu(router, store);//初始化菜单
        if (!window.sessionStorage.getItem("user")) {
            return GetRequest("/admin/info").then(response => {
                if (response) {
                    window.sessionStorage.setItem("user", JSON.stringify(response));
                    store.commit("INIT_ADMIN", response);
                    next();
                }
            });
        }
        next();
    } else {
        if (to.path == '/') {
            next();
        } else {
            next("/?redirect=" + to.path);
        }
    }
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
